//
//  TalkfunOptionsTableViewCell.swift
//  test
//
//  Created by 莫瑞权 on 2021/12/10.
//

import UIKit

//判断题
class TalkfunOptionsDecideCollectionViewCell: UICollectionViewCell {
    /**2:随堂考 3.问卷 4.快速答题卡*/
    @objc public var subjectExamineInfoType:TFExaminationType = TFExaminationTypeTestPaper
    @objc public let answerTextLabel:UILabel = UILabel.init()
    //表单选中
    @objc public let selectForm:UIImageView = UIImageView()
    //内容
    @objc public let textLabel:UILabel = UILabel.init()
    @objc public let point:UIImageView = UIImageView.init()
  
    override init(frame: CGRect) {
        super.init(frame: frame)
        //填空题的答案
        let attrString = NSMutableAttributedString(string: "【答案】")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 16) as Any,.foregroundColor: UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), ]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        self.answerTextLabel.attributedText = attrString
        self.answerTextLabel.numberOfLines = 0
        self.contentView.addSubview(self.answerTextLabel)
        self.answerTextLabel.backgroundColor = UIColor.clear
        self.contentView.addSubview(self.textLabel)
        self.point.isHidden = true;
        self.addSubview(self.point)
        
        self.addSubview(self.selectForm)
    }
    override func layoutSubviews() {
        super.layoutSubviews()

        self.answerTextLabel.frame = CGRect(x: 0, y: 5, width: 64, height: 23)
        let pointW = 15.0
        let pointH = pointW
        let pointX = self.frame.size.width - pointW - pointH
        let pointY = (self.frame.size.height - pointH)/2.0
        self.point.frame = CGRect(x: pointX, y: pointY, width: pointW, height: pointH)
        
    }
    
    func updateUI(model:TalkfunSubjectAnswer ,type:TalkfunTopicType,isEndOfTestPaper:Bool ,row:NSInteger)
    
    {
        
        self.point.isHidden = true
        self.layer.borderColor = UIColor.clear.cgColor
        self.layer.borderWidth = 0
        self.layer.cornerRadius = 4
        self.layer.masksToBounds = true
        self.textLabel.numberOfLines = 0
        let rom = NSString(format: "%ld", row + 1) as String;
        var msg = TalkfunSharedTools.asciiBytesToString(inx: row) + "." + model.answer
        //表单
        if self.subjectExamineInfoType == TFExaminationTypeTable {
            msg = model.answer
        }
        
        self.selectFormImage(type: type, isSelected: model.isSelected)
        
        var titleAttrString = NSMutableAttributedString(string: (msg as NSString) as String)
        
        //"填空题"
        if (type == TalkfunTopicTypeStuff )
        {
            
            msg = "(" + rom + ") " + model.answer
            titleAttrString = NSMutableAttributedString(string: (msg as NSString) as String)
            if (row == 0)
            {
                self.answerTextLabel.isHidden = false
            }else{
                self.answerTextLabel.isHidden = true
            }
            
            self.backgroundColor = UIColor.clear
            
            let titleAttr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 16)as Any,.foregroundColor: UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1),]
            
              titleAttrString.addAttributes(titleAttr, range: NSRange(location: 0, length: titleAttrString.length))
            

        }else{
            self.answerTextLabel.isHidden = true
            //已经交卷了&&用户选中的选项
            if(isEndOfTestPaper )
            {
                
                //用户选中的
                if(model.isSelected){
                    //是否正确
                    if(model.isCorrect)
                    {
                        
                        self.backgroundColor = UIColor(red: 0.24, green: 0.78, blue: 0.4, alpha: 1)
                        
                        let titleAttr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 16)as Any,.foregroundColor: UIColor(red: 1, green: 1, blue: 1,alpha:1)]
                          titleAttrString.addAttributes(titleAttr, range: NSRange(location: 0, length: titleAttrString.length))
                        self.point.image = UIImage.init(named: "HTLive.bundle/TFquan")
                        self.point.isHidden = false
                    }
                    else
                    {
                        
                        
                        var backgroundColor:UIColor = UIColor.white
                        var foregroundColor:UIColor = UIColor.white
                        //测试
                    if(self.subjectExamineInfoType == TFExaminationTypeTestPaper) {
                        self.point.image = UIImage.init(named: "HTLive.bundle/TFcha")
                            backgroundColor =  UIColor(red: 1, green: 0.33, blue: 0.33, alpha: 1)
                          foregroundColor = UIColor(red: 1, green: 1, blue: 1,alpha:1)
                        
                    }else  if(self.subjectExamineInfoType == TFExaminationTypeTable) {
                           //表单
                            backgroundColor = UIColor(red: 0.88, green: 0.94, blue: 1, alpha: 1)
                        //lab颜色
                        foregroundColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0,alpha:1)
                        self.point.image = UIImage.init(named: "HTLive.bundle/")
                    }
                        self.backgroundColor = backgroundColor
                        
                        
                        let titleAttr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 16)as Any,.foregroundColor: foregroundColor]
                          titleAttrString.addAttributes(titleAttr, range: NSRange(location: 0, length: titleAttrString.length))
                       
                        self.point.isHidden = false

                    }
                }
                
                else
                {
                    //用户未选中的 &&但正确 && 多选题
        
                    if(model.isCorrect && type == TalkfunTopicTypeMultiple )
    
                    {
    
                        self.backgroundColor = UIColor(red: 0.91, green: 0.98, blue: 0.95, alpha: 1)
                        let titleAttr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 16)as Any,.foregroundColor: UIColor(red: 0.05, green: 0.73, blue: 0.25,alpha:1),]
                          titleAttrString.addAttributes(titleAttr, range: NSRange(location: 0, length: titleAttrString.length))
                    }else
                    {
                        self.backgroundColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 1)
                        let titleAttr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 16)as Any,.foregroundColor: UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1),]
                          titleAttrString.addAttributes(titleAttr, range: NSRange(location: 0, length: titleAttrString.length))
                    }
                   
                }
                
            }
            else
            {
                //未结束
           
                if(model.isSelected){
                    self.layer.borderWidth = 1
                    self.layer.borderColor = UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1).cgColor

                    //蓝色
                    self.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
                    let titleAttr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 16)as Any,.foregroundColor: UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1),]
                      titleAttrString.addAttributes(titleAttr, range: NSRange(location: 0, length: titleAttrString.length))

                   
                    
                }else
                {
                    //灰色
                    self.backgroundColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 1)
                    let titleAttr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 16)as Any,.foregroundColor: UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1),]
                      titleAttrString.addAttributes(titleAttr, range: NSRange(location: 0, length: titleAttrString.length))

                    

                }
              
            }
         
           
            
        }
        
        var textLabelX = 16.0
        var textLabelY = 11.0
        var textLabelW = self.frame.width - textLabelX - textLabelX
        //填空题
        if (type == TalkfunTopicTypeStuff )
        {
            textLabelX = 64.0
            textLabelY = 5.0
            textLabelW = self.frame.width - textLabelX
        }
        self.selectForm.isHidden = true
        //表单
        if self.subjectExamineInfoType == TFExaminationTypeTable {
            textLabelX = 38.0
            textLabelW = self.frame.width - textLabelX - 16.0
            self.selectForm.isHidden = false
            self.selectForm.frame = CGRect(x: 16.0, y: (self.frame.height - 16.0)/2.0, width: 16.0, height: 16.0)
        }
        
        let sizeLabel = TalkfunSharedTools.getSizeLabel(labelString: titleAttrString, labelWidth: textLabelW , labelfont: UIFont(name: "PingFang SC", size: 16)! )
        
        self.textLabel.frame = CGRect(x: textLabelX, y: textLabelY, width: textLabelW, height: sizeLabel.height)
        
        self.textLabel.attributedText = titleAttrString
        self.textLabel.lineBreakMode = NSLineBreakMode.byCharWrapping;
        self.textLabel.backgroundColor = UIColor.clear
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///**type 1单选题、2多选题、3判断题、4填空题、5简答题         isSelected:选中 */
    func selectFormImage(type:TalkfunTopicType,isSelected:Bool)
    {
        if(isSelected){

            if (type == TalkfunTopicTypeIndividualEvent) {
                self.selectForm.image = UIImage.init(named: "HTLive.bundle/TF_checked")
            }else if (type == TalkfunTopicTypeMultiple) {
                self.selectForm.image = UIImage.init(named: "HTLive.bundle/TF_checked-more")
            }
        }else
        {
            if (type == TalkfunTopicTypeIndividualEvent) {
                self.selectForm.image = UIImage.init(named: "HTLive.bundle/TF_uncheck")
            }else if (type == TalkfunTopicTypeMultiple) {
                
                self.selectForm.image = UIImage.init(named: "HTLive.bundle/TF_unckecked-2")
            }
        }
        
        
    }
}
